package cn.bdqn.asecu.controller;

import cn.bdqn.asecu.service.Auecu_lineService;
import cn.bdqn.asecu.utility.Utility_Class;
import cn.bdqn.asecu.utility.Utility_departmetn;
import cn.bdqn.pojo.Department;
import cn.bdqn.pojo.Maxdepartment;
import cn.bdqn.pojo.Role;
import cn.bdqn.pojo.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 用户模块
 *
 */
@Controller
public class Auecu_lineController {

    @Resource
    private Auecu_lineService auecu_lineService;
    //用户总页面
    @RequestMapping("user-list")
    public String entrance(){
        return "user-list";
    }

    //用户添加页面
    @RequestMapping("user-add")
    public  String showadd(int id, Model model){
        Department department = auecu_lineService.departmentSel(id);
        model.addAttribute("department",department);
        return "user-add";
    }
    //用户显示
    @RequestMapping("UserShow")
    @ResponseBody
    public  List<Utility_Class>  UserShow(Integer id,Integer pdid){
        if (id!=null){
        List<User> users = auecu_lineService.SelAll(id,pdid);
        List<Utility_Class> uy=new ArrayList<>();
       for (User u:users) {
           Utility_Class utility_class=new Utility_Class();
           utility_class.setUsers(u);
         Department department = auecu_lineService.departmentSel(u.getDepartmentId());
         Role permissionquery = auecu_lineService.Permissionquery(u.getRoleId());;
         utility_class.setDepartmentname(department.getName());
         utility_class.setRolename(permissionquery.getName());
           uy.add(utility_class);
        }
            return uy;
        }
        return null;
    }

    //树形显示部门
    @RequestMapping("Seldepartment")
    @ResponseBody
    public List<Utility_departmetn> departmentShow(){
        List<Utility_departmetn> utility_departmetns = auecu_lineService.SeldepartmentShow();
        return utility_departmetns;
    }

    //树形总部
    @RequestMapping("Selzgeneral")
    @ResponseBody
    public  List<Maxdepartment> Selzgeneral(){
         return auecu_lineService.Selzgeneral();
    }

    //部门显示
    @RequestMapping("AdSelall")
    @ResponseBody
    public List<Department> Adepartment(){
        List<Department> departments = auecu_lineService.departmentSelAll();
        return departments;
    }

    //用户添加
    @RequestMapping("addUser")
    @ResponseBody
    public  User  addUser(User user){
        return  auecu_lineService.insetUser(user);
    }

    //用户批量删除
    @PostMapping("delUser")
    public String delUser(Integer[] items_id){
        for (Integer i: items_id) {
           auecu_lineService.delUser(i);
        }
        return "user-list";
    }

    //重置密码页面
    @RequestMapping("reset-pwd")
    public String resetpassword(Integer id,Model model){
        User user = auecu_lineService.SelUser(id);
        model.addAttribute("Uid",user);
        return "reset-pwd";
    }

    //重置密码页面
    @RequestMapping("reset_upl")
    @ResponseBody
    public boolean reset_upl(User user){
        System.out.println(user);
        int i = auecu_lineService.uplUserpasswrd(user);
        return true;
    }

    //状态
    @RequestMapping("stateUpl")
    public String stateUpl(Integer[] items_id){
        if (items_id!=null) {
            for (Integer i : items_id) {
                User user = auecu_lineService.SelUser(i);
                if (user.getRoleId()!=2) {
                    if (user.getState().equals("0")) {
                        int upluserstate = auecu_lineService.upluserstate(i, "1");
                    } else {
                        int upluserstate = auecu_lineService.upluserstate(i, "0");
                    }
                }
            }
            return "user-list";
        }
        return "user-list";
    }

    //编辑页面
    @RequestMapping("user-edit")
    public String useredit(Integer id,Model model){
        User user = auecu_lineService.SelUser(id);
        Department department = auecu_lineService.departmentSel(id);
        model.addAttribute("bu",department);
        model.addAttribute("eu",user);
        return "user-edit";
    }

    //编辑
    @RequestMapping("upl_edit")
    @ResponseBody
    public int upl_edit(User user){
        int uplportion = auecu_lineService.uplportion(user);
        System.out.println(uplportion);
        return uplportion;
    }

    //设置角色
    @RequestMapping("user_role")
    public String settherole(Integer id,Model model){
        User user = auecu_lineService.SelUser(id);
        model.addAttribute("setUser",user);
        return "user_role";
    }

    //权限显示
    @RequestMapping("setshow_roel")
    @ResponseBody
    public List<Role> setshow_roel(){
        List<Role> roles = auecu_lineService.SelRoelAll();
        return roles;
    }

    //修改rooel
    @RequestMapping("uplroel")
    @ResponseBody
    public int uplroel(User user){
        System.out.println(user);
     return auecu_lineService.uplRoot(user);
    }

}
